package com.ftg.elvis.controller;

import com.ftg.elvis.pojo.Apply;
import com.ftg.elvis.service.AdminApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/apply")
public class AdminApplyController {

	@Autowired
	AdminApplyService adminApplyService;

	@GetMapping("/showAll")
	public Map<String, Object> showAll(int page, int limit, String aname, String astate){
		List<Apply> list =  adminApplyService.showAll(page, limit, aname, astate);
		Map<String, Object> map = new HashMap<>();
		map.put("aname", aname);
		map.put("astate", astate);
		map.put("code", 0);
		map.put("count", adminApplyService.getCount(aname, astate));
		map.put("data", list);
		return map;
	}

	@GetMapping("/showState")
	public Map<String, Object> findByState(int page,int limit, String astate){
		List<Apply> list =  adminApplyService.findByState(page, limit, astate);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("count", adminApplyService.getCountByState(astate));
		map.put("data", list);
		return map;
	}

	@PostMapping("/update")
	public Map<String, Object> updateState(String astate, String aid){
		Map<String, Object> map = new HashMap<>();
		try {
			int flag = adminApplyService.updateState(astate, aid);
			map.put("code", 0);
			map.put("flag",flag);
		} catch (Exception e) {
			map.put("code",1);
			e.printStackTrace();
		}
		return map;
	}

	@DeleteMapping("/del/{aid}")
	public int delete(@PathVariable String aid){
		return adminApplyService.delete(aid);
	}
}
